package com.dreamstations.changshads.fragment;


import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.dreamstations.changshads.R;
import com.dreamstations.changshads.activity.ArticleDetailActivity;
import com.dreamstations.changshads.activity.ArticleDetail_WebActivity;
import com.dreamstations.changshads.adapter.DSRvAdapter;
import com.dreamstations.changshads.entity.StoryBean;
import com.dreamstations.changshads.util.DSRequest;
import com.dreamstations.changshads.util.DSUtils;
import com.dreamstations.changshads.util.RequestType;
import com.dreamstations.changshads.view.Slide_Indicator;
import com.squareup.picasso.Picasso;
import com.zhy.base.adapter.ViewHolder;
import com.zhy.base.adapter.recyclerview.OnItemClickListener;

import java.util.ArrayList;
import java.util.List;

/**
 * 口述长沙
 */
public class Story_Header_Frag extends BaseFragmentForSection<StoryBean> {

    public Story_Header_Frag() {
    }

    @SuppressLint("ValidFragment")
    public Story_Header_Frag(String title, @RequestType String reqType, int category) {
        Bundle bundle = new Bundle();
        bundle.putString("title", title);
        bundle.putString("reqType", reqType);
        bundle.putInt("category", category);
        setArguments(bundle);
    }

    Picasso picasso ;
    List<String> headStrings;

    @SuppressWarnings("WrongConstant")
    @NonNull
    @Override
    public DSRequest getDsRequest() {
        Bundle bundle = getArguments();
        return new DSRequest()
                .type(bundle.getString("reqType"))
                .addParams("page",String.valueOf(1))
                //二级分类
                .setTag(Story_Header_Frag.this)
                .addParams("typeId", String.valueOf(bundle.getInt("category")));
    }

    @NonNull
    @Override
    public DSRvAdapter.RvSupport<StoryBean> getItem() {
        return new DSRvAdapter.RvSupport<StoryBean>() {

            @Override
            public int getItemViewType(StoryBean data) {
                return 0;
            }

            @Override
            public ViewHolder createHolder(Context context, ViewGroup parent, int viewType) {
                return ViewHolder.get(context, null, parent, R.layout.story_information_item, -1);
            }

            @Override
            public void convert(ViewHolder holder, StoryBean data) {
                holder.setText(R.id.textView7,data.getTitle());
                holder.setText(R.id.item_time,data.getTime());
                if (data.getCover() == null || data.getCover().equals("")) {  //没有封面
                    return ;
                }else {
                    ImageView imageView = holder.getView(R.id.item_cover);
                    Picasso.with(getActivity())
                            .load(DSRequest.SERVER_BASE1 + data.getCover())
                            .error(R.drawable.home_nav_header)
                            .into(imageView);
                }
            }
        };
    }

    @Override
    public OnItemClickListener getOnItemClickListener() {
        return new OnItemClickListener<StoryBean>() {
            @Override
            public void onItemClick(ViewGroup parent, View view, StoryBean o, int position) {
                Intent intent;
                if (o.getTemplate() == 1) {
                    intent = new Intent(getActivity(), ArticleDetail_WebActivity.class);
                    intent.putExtra("data", o);
                } else {
                    intent = new Intent(getActivity(), ArticleDetailActivity.class);
                    intent.putExtra("data", o);
                }
                startActivity(intent);
            }

            @Override
            public boolean onItemLongClick(ViewGroup parent, View view, StoryBean o, int position) {
                return false;
            }
        };
    }
    ViewPager viewPager;
    TextView textView;
    @Override
    public DSRvAdapter.RvSupport getHeader() {
        picasso = Picasso.with(getContext());
        Bundle bundle = getArguments();
        headStrings=new ArrayList<>();
        final List<ImageView> imageViews = new ArrayList<ImageView>();
        for (int i = 0; i < 5; i++) {
            ImageView imageView = new ImageView(this.getActivity());
            headStrings.add("");
            // 异步加载图片
           // picasso.load("http://i1.buimg.com/3ee901bfed3694d3s.png").into(imageView);
            imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
            imageViews.add(imageView);
        }
        new DSRequest()
                .type(bundle.getString("reqType"))
                .addParams("page",String.valueOf(1))
                .setTag(Story_Header_Frag.this)
                //二级分类
                .addParams("typeId", String.valueOf(bundle.getInt("category")))
                .addParams("front",String.valueOf(1))

                .build()
                .execute(new DSRequest.DSCallback<List<StoryBean>>() {
                    @Override
                    public void onResponse(List<StoryBean> result) {
                        if(textView!=null) {
                            textView.setText(result.get(viewPager.getCurrentItem()).getTitle());
                        }
                        for(int i=0;i<result.size();i++){
                            picasso.load(DSRequest.SERVER_BASE1+result.get(i).getCover()).error(R.drawable.home_nav_header).into(imageViews.get(i));
                            final StoryBean storyBean=result.get(i);
                            headStrings.set(i,result.get(i).getTitle());
                            //L.e("tagfigureh",DSRequest.SERVER_BASE1+result.get(i).getCover());
                            imageViews.get(i).setOnClickListener(new View.OnClickListener() {
                                @Override
                                public void onClick(View v) {
                                    Intent intent;
                                    if (storyBean.getTemplate() == 1) {
                                        intent = new Intent(getActivity(), ArticleDetail_WebActivity.class);
                                        intent.putExtra("data", storyBean);
                                    } else {
                                        intent = new Intent(getActivity(), ArticleDetailActivity.class);
                                        intent.putExtra("data", storyBean);
                                    }
                                    startActivity(intent);
                                }
                            });
                        }
                    }

                    @Override
                    public void onFailure(String msg) {
                    }
                });
        DSRvAdapter.RvSupport header = new DSRvAdapter.RvSupport() {
            @Override
            public int getItemViewType(Object data) {
                return 0;
            }

            @Override
            public ViewHolder createHolder(Context context, ViewGroup parent, int viewType) {
                ViewHolder holder = ViewHolder.get(context, null, parent, R.layout.simple_vpheader, -1);
                viewPager = holder.getView(R.id.header_viewpager);
                textView=holder.getView(R.id.header_title);
                viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
                    @Override
                    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
                    }

                    @Override
                    public void onPageSelected(int position) {
                        textView.setText(headStrings.get(position));
                    }

                    @Override
                    public void onPageScrollStateChanged(int state) {
                    }
                });
                Slide_Indicator indicator = holder.getView(R.id.header_indicator);
                PagerAdapter pagerAdapter = new PagerAdapter() {
                    @Override
                    public int getCount() {
                        return imageViews.size();
                    }

                    @Override
                    public boolean isViewFromObject(View view, Object object) {
                        return view == object;
                    }

                    @Override
                    public Object instantiateItem(ViewGroup container, int position) {
                        container.addView(imageViews.get(position));
                        return imageViews.get(position);
                    }

                    @Override
                    public void destroyItem(ViewGroup container, int position, Object object) {
                        container.removeView(imageViews.get(position));
                    }
                };
                viewPager.setAdapter(pagerAdapter);
                indicator.setupWithViewPager(viewPager);
                return holder;
            }

            @Override
            public void convert(ViewHolder holder, Object data) {

            }

        };
        return header;
    }

    @Override
    public boolean getmEnablePullLoad() {
        return true;
    }
}
